var m3031root = document.getElementById("m3031root");
function m3031Init()
{
	var qid = localStorage.getItem("m303qid");
	if(qid==null||qid=='') 
		mui.alert('无效跳转','提示','确认',function (e) { mui.back(); },'div');
	var title = localStorage.getItem("m303title");
	if(title==null) 
		mui.alert('无效跳转','提示','确认',function (e) { e.index },'div');
	
	ID("m3031h1").innerHTML = title;
	
	var mdata = {
		'qid':qid,
	};
	
	m3031ajax(mdata);
}
function m3031ajax(mdata)
{
	mui.ajax(ASYURI+'A1015.do',{
		data:mdata,
		dataType:'json',//服务器返回json格式数据
		type:'post',//HTTP请求类型
		timeout:10000,//超时时间设置为10秒；
		success:function(data){
			if(data.tag=='2')
			{
				mui.toast("该问卷还没有人回答过哦");
				return;
			}
			var s1 = data[1];
			var s2 = data[2];
			var le = s1.length;
			for(si=0;si<le;si++)
			{
				console.log(s1[si][si+1]);
				var info = s1[si][si+1];
				if(info.type=='C')
				{
					var card = cardInit();
					var ch = chInit(si+1+"."+info.body);
					card.appendChild(ch);
					var cf = cfInit();
					var contains = document.createElement("div");
					cf.appendChild(contains);
					
					var answerSet = s2[si+1];
					var aslen = Object.keys(answerSet).length;
					for(mi=1;mi<=aslen;mi++)
					{
						contains.appendChild(pInit(mi+"."+answerSet[mi]));
					}
					card.appendChild(cf);
					m3031root.appendChild(card);
				}
				else{
					
					var card = cardInit();
					var ch = chInit(si+1+"."+info.body);
					var infolen = Object.keys(info).length-2;
					for(mi=0;mi<infolen;mi++)
					{
						if(mi==0) ch.appendChild(pInit("A."+info.A));
						else if(mi==1) ch.appendChild(pInit("B."+info.B));
						else if(mi==2) ch.appendChild(pInit("C."+info.C));
						else if(mi==3) ch.appendChild(pInit("D."+info.D));
					}
					card.appendChild(ch);
					
					var cf = cfInit();
					var contains = document.createElement("div");
					cf.appendChild(contains);
					
					var answerSet = s2[si+1];
					var aslen = Object.keys(answerSet).length;
					
					var cb = cbInit("A"+si);
					card.appendChild(cb);
					card.appendChild(cf);
					m3031root.appendChild(card);
					
					var ss2 = s2[si+1];
					var redata = new Array();
					var reindex = 0;
					
					redata[reindex++] = dataReform("A",ss2.A);
					redata[reindex++] = dataReform("B",ss2.B);
					redata[reindex++] = dataReform("C",ss2.C);
					redata[reindex++] = dataReform("D",ss2.D);
					redata[reindex++] = dataReform("AB",ss2.AB);
					redata[reindex++] = dataReform("AC",ss2.AC);
					redata[reindex++] = dataReform("AD",ss2.AD);
					redata[reindex++] = dataReform("BC",ss2.BC);
					redata[reindex++] = dataReform("BD",ss2.BD);
					redata[reindex++] = dataReform("CD",ss2.CD);
					redata[reindex++] = dataReform("ABC",ss2.ABC);
					redata[reindex++] = dataReform("ABD",ss2.ABD);
					redata[reindex++] = dataReform("ACD",ss2.ACD);
					redata[reindex++] = dataReform("BCD",ss2.BCD);
					redata[reindex++] = dataReform("ABCD",ss2.ABCD);
					console.log(redata);
					
					var ochart = echarts.init(document.getElementById("A"+si)).setOption({
						backgroundColor: '#2c343c',
						tooltip : {
							trigger: 'item',
							formatter: "{b} : {c} ({d}%)"
						},
						series : [{
							type:'pie',
							radius:'55%',
							roseType:'angle',
							data : redata,
						}]
						
					});
					
				}
			}
			
		},
		error:function(xhr,type,errorThrown){ }
	});
}
function dataReform(ikey,ivalue)
{
	if(ivalue==undefined)
	return {};
	else
		return {value:ivalue,name:ikey};
		
}
function cardInit()
{
	var card = document.createElement("div");
	card.setAttribute("class","mui-card");
	return card;
}
function chInit(cVal)
{
	var ch = document.createElement("div");
	ch.setAttribute("class","mui-card-header");
	ch.innerHTML=cVal;
	return ch;
}
function cfInit()
{
	var cf = document.createElement("div");
	cf.setAttribute("class","mui-card-footer");
	return cf;
}
function cbInit(cbVal)
{
	var cb = document.createElement("div");
	cb.setAttribute("id",cbVal);
	cb.setAttribute("class","mui-card-content");
	cb.setAttribute("style","width:300px;height:300px;");
	return cb;
}

function pInit(pVal)
{
	var p = document.createElement("p");
	p.innerHTML = pVal;
	return p;
}
function label(value)
{
	var vlabel = document.createElement("label");
	vlabel.innerHTML = value;
	return vlabel;
}

function ID(id)
{
	return document.getElementById(id);
}
m3031Init();